package com.leetcode.problems072;

/**
 * https://leetcode-cn.com/problems/edit-distance/
 * @Date 2021/12/09 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {

    /**
     * if w[i] != w2[j]
     *      dp[i][j] = min(dp[i - 1][j - 1] + 1 , dp[i - 1][j] + 1 , dp[i][j - 1] + 1)

     * @param word1
     * @param word2
     * @return
     */
    public int minDistance(String word1, String word2) {
        int l1 = word1.length();
        int l2 = word2.length();
        if(l1 == 0 && l2 == 0){
            return 0;
        }
        int[][] dp = new int[l1 + 1][l2 + 1];

        for (int i = 0; i <= l1; i++) {
            dp[i][0] = i;
        }

        for (int i = 0; i <= l2; i++) {
            dp[0][i] = i;
        }


        for (int i = 1; i <= l1; i++) {
            for (int j = 1; j <= l2; j++) {
                if(word1.charAt(i - 1) == word2.charAt(j - 1)){
                    dp[i][j] = dp[i - 1][j-1];
                }else{
                    dp[i][j] = 1 + Math.min(dp[i - 1][j - 1],Math.min(dp[i - 1][j], dp[i][j - 1]));
                }
            }
        }

        return dp[l1][l2];
    }

    public static void main(String[] args) {
        String word1 = "intention";
        String word2 = "execution";
        System.out.println(new Solution().minDistance(word1,word2));
    }
}
